layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/transportMsg/transportMsg?dispatchNumber=" + Feng.getUrlParam("dispatchNumber"));
    var result = ajax.start();
	
	var html = '';
    for (var i = 0; i < result.data.length; i++) {
		html += '<li class="layui-timeline-item">'+
					    '<i class="layui-icon layui-timeline-axis">&#xe63f;</i>'+
					    '<div class="layui-timeline-content layui-text">'+
					      '<h3 class="layui-timeline-title">'+result.data[i].status+'-'+result.data[i].msgType+'</h3>'+
					      '<p>'+result.data[i].content
		if(result.data[i].status == '客户签收' && result.data[i].v4 != '' && result.data[i].v4 != null){
			html+= '<br><a class="ul-nodes-a" style="color: #1999dc;" v4="'+result.data[i].v4+'" onclick="#">查看回单</a>'+
					      '</p>'+
					      '<em>'+result.data[i].times+result.data[i].createUsername+'</em>'+
					    '</div>'+
					  '</li>'
		}else if(result.data[i].status == '送达' && result.data[i].v4 != '' && result.data[i].v4 != null){
			html+= '<br><a class="ul-nodes-a" style="color: #1999dc;" v4="'+result.data[i].v4+'" onclick="#">查看签名</a>'+
					      '</p>'+
					      '<em>'+result.data[i].times+result.data[i].createUsername+'</em>'+
					    '</div>'+
					  '</li>'
		} else {
			html += '</p>'+
					      '<em>'+result.data[i].times+result.data[i].createUsername+'</em>'+
					    '</div>'+
					  '</li>'
		}
    }
	$("#ul-nodes").append(html)
	
	$(".ul-nodes-a").on("click",function(){
		//构建图像数组
		var items = [];
		var obj = {};
		obj.src = '/transportReceipt/show?fileId=' + this.getAttribute('v4');
		obj.title = '预览图片';
		items.push(obj)
	  	//定义选项（如果需要）
	  	var options = {
	  	    index: 0 // 此选项表示您将从第一张图片开始
	  	};
	  	//初始化插件
	  	var viewer = new PhotoViewer(items, options);
	});
	
    /**
     * 运输信息表管理
     */
    var TransportMsg = {
        tableId: "transportMsgTable"
    };

    /**
     * 初始化表格的列
     */
    TransportMsg.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键id'},
            {field: 'dispatchNumber', sort: true, title: '运输单号'},
            {field: 'status', sort: true, title: '状态'},
            {field: 'msgType', sort: true, title: '类型'},
            {field: 'content', sort: true, title: '内容'},
            {field: 'times', sort: true, title: '时间'},
            {field: 'deptId', sort: true, title: '部门id'},
            {field: 'deliverId', sort: true, title: '交付客户id'},
            {field: 'v1', sort: true, title: 'v1'},
            {field: 'v2', sort: true, title: 'v2'},
            {field: 'v3', sort: true, title: 'v3'},
            {field: 'v4', sort: true, title: 'v4'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    TransportMsg.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(TransportMsg.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
                 // 搜索按钮点击事件
                 $('#btnSearch').click(function () {
                     TransportMsg.search();
                 });
                 // 添加按钮点击事件
                 $('#btnAdd').click(function () {
                     TransportMsg.openAddDlg();
                 });
                 // 导出excel
                 $('#btnExp').click(function () {
                     TransportMsg.exportExcel();
                 });
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    TransportMsg.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/transportMsg/add';
    };

    /**
     * 导出excel按钮
     */
    TransportMsg.exportExcel = function () {
        var checkRows = table.checkStatus(TransportMsg.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    TransportMsg.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/transportMsg/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    TransportMsg.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/transportMsg/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(TransportMsg.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    /*var tableResult = table.render({
        elem: '#' + TransportMsg.tableId,
        url: Feng.ctxPath + '/transportMsg/list',
        page: true,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: TransportMsg.initColumn()
    });*/

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TransportMsg.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        TransportMsg.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        TransportMsg.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + TransportMsg.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            TransportMsg.openEditDlg(data);
        } else if (layEvent === 'delete') {
            TransportMsg.onDeleteItem(data);
        }
    });
});
